﻿using System.Diagnostics;
#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
using Windows.System;
using Windows.UI.Xaml.Controls.Primitives;
#else
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
#endif

namespace FreshMeat.Xaml
{
   public class SecretKeyBehavior : AttachedBehavior<SecretKeyBehavior, FrameworkElement>
   {
      /// <summary>
      /// Indicates the target to be shown/hidden when the secret key is pressed.
      /// </summary>
      public readonly static DependencyProperty TargetProperty = DependencyProperty.RegisterAttached(
         "Target",
         typeof(FrameworkElement),
         typeof(SecretKeyBehavior),
         // ReSharper disable RedundantCast
         // Cast of null is critical -- WPF doesn't work without it!
         new FrameworkPropertyMetadata((FrameworkElement)null, (d, e) => GetInstance(d)));
         // ReSharper restore RedundantCast

      public static FrameworkElement GetTarget(DependencyObject d)
      {
         return (FrameworkElement)d.GetValue(TargetProperty);
      }

      public static void SetTarget(DependencyObject d, FrameworkElement element)
      {
         d.SetValue(TargetProperty, element);
      }

      protected override void OnAttached()
      {
         var associatedObject = TryGetAssociatedObject();
         associatedObject.KeyDown += OnAssociatedObjectKeyDown;
      }

      protected override void OnDetaching()
      {
         var associatedObject = TryGetAssociatedObject();
         if (associatedObject == null)
            return;

         associatedObject.KeyDown -= OnAssociatedObjectKeyDown;
      }

#if NETFX_CORE
      void OnAssociatedObjectKeyDown(object sender, KeyRoutedEventArgs e)
#else
      void OnAssociatedObjectKeyDown(object sender, KeyEventArgs e)
#endif
      {
         var associatedObject = TryGetAssociatedObject();
         if (associatedObject == null)
            return;

         var target = GetTarget(associatedObject);
         if (target == null)
            return;

#if NETFX_CORE
         if (e.Key != VirtualKey.F12)
#else
         if (e.Key != Key.F12 || (Keyboard.Modifiers != (ModifierKeys.Control | ModifierKeys.Shift)))
#endif
            return;

         if (!FocusHelper.IsFocusWithin(associatedObject))
            return;

         if (target is Popup)
         {
            var popup = target as Popup;
            var newValue = !popup.IsOpen;
            Trace.TraceInformation(string.Format("SecretKeyBehavior is setting Popup.IsOpen={0}", newValue));
            popup.IsOpen = newValue;
         }
         else
         {
            var newValue = target.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
            Trace.TraceInformation(string.Format("SecretKeyBehavior is setting target.Visibility={0}", newValue));
            target.Visibility = newValue;
         }
      }
   }
}
